package htmodels

import (
	"github.com/astaxie/beego/orm"
	"shuxianghui/models"
)

func HTArticleAdd(article *models.Article) error {
	o := orm.NewOrm()
	_, err := o.Insert(article)
	return err
}

func HTArticleGetDetail(article *models.Article) error {
	o := orm.NewOrm()
	return o.Read(article)
}

type CityId_int int

func HTArticleGetCount(search string, searchFiled string, Status int, ArticleType int, cityid int, uid int, extends ...interface{}) (int64, error) {
	o := orm.NewOrm()
	m := models.Article{}
	flagCity := true
	qurybuilder := o.QueryTable(m.TableName())
	for _, arg := range extends {
		//迭代不定参数
		switch arg.(type) {
		case []CityId_int:
			if len(arg.([]CityId_int)) > 0 {
				flagCity = false
				qurybuilder = qurybuilder.Filter(m.FN_CityId() + "__in", arg.([]CityId_int))
			}
		}
	}
	if cityid > 0 && flagCity {
		qurybuilder = qurybuilder.Filter(m.FN_CityId(), cityid)
	}
	if uid > 0 {
		qurybuilder = qurybuilder.Filter(m.FN_UserId(), uid)
	}
	if search != "" {
		qurybuilder = qurybuilder.Filter(searchFiled + "__icontains", search)
	}
	if Status >= 0 {
		qurybuilder = qurybuilder.Filter(m.FN_Status(), Status)
	}
	if ArticleType >= 0 {
		qurybuilder = qurybuilder.Filter(m.FN_ArticleType(), ArticleType)
	}

	count, err := qurybuilder.Count()
	return count, err
}

func HTArticleGetList(articles *[]models.Article, fields []string, Status int, ArticleType int, offset int64, limit int, search string, searchFiled string, orderby string, cityid int, uid int, extends ...interface{}) error {
	o := orm.NewOrm()
	m := models.Article{}
	flagCity := true
	qurybuilder := o.QueryTable(m.TableName())
	for _, arg := range extends {
		//迭代不定参数
		switch arg.(type) {
		case []CityId_int:
			if len(arg.([]CityId_int)) > 0 {
				flagCity = false
				qurybuilder = qurybuilder.Filter(m.FN_CityId() + "__in", arg.([]CityId_int))
			}
		}
	}
	if cityid > 0 && flagCity {
		qurybuilder = qurybuilder.Filter(m.FN_CityId(), cityid)
	}
	if uid > 0 {
		qurybuilder = qurybuilder.Filter(m.FN_UserId(), uid)
	}
	if search != "" {
		qurybuilder = qurybuilder.Filter(searchFiled + "__icontains", search)
	}
	if Status >= 0 {
		qurybuilder = qurybuilder.Filter(m.FN_Status(), Status)
	}
	if ArticleType >= 0 {
		qurybuilder = qurybuilder.Filter(m.FN_ArticleType(), ArticleType)
	}
	_, err := qurybuilder.OrderBy(orderby).Offset(offset).Limit(limit).All(articles, fields...)
	return err
}

func HTArticleDelete(Ids []int64) (int64, error) {
	o := orm.NewOrm()
	m := models.Article{}
	count, err := o.QueryTable(m.TableName()).Filter("Id" + "__in", Ids).Exclude(m.FN_ArticleType(), 3).Delete()
	return count, err
}

func HTArticleStatusChange(Ids []int64, status int) (int64, error) {
	o := orm.NewOrm()
	m := models.Article{}
	count, err := o.QueryTable(m.TableName()).Filter("Id" + "__in", Ids).Update(orm.Params{
		m.FN_Status(): status,
	})
	return count, err
}
func HTArticleArticleTypeChange(Ids []int64, articletype int) (int64, error) {
	o := orm.NewOrm()
	m := models.Article{}
	count, err := o.QueryTable(m.TableName()).Filter("Id" + "__in", Ids).Update(orm.Params{
		m.FN_ArticleType(): articletype,
	})
	return count, err
}

func HTArticleUpdate(article *models.Article) error {
	o := orm.NewOrm()
	_, err := o.Update(article)
	return err
}

func HTArticleGetDushuByCity(article *models.Article) error {
	o := orm.NewOrm()
	return o.QueryTable(article.TableName()).Filter(article.FN_CityId(), article.CityId).Filter(article.FN_ArticleType(), 3).One(article)
}

func HTArticleGetMyArticleInfo(uid int, city int, status int, extends ...interface{}) (int64, error) {
	o := orm.NewOrm()
	m := models.Article{}
	flagCity := true
	qs := o.QueryTable(m.TableName())
	for _, arg := range extends {
		//迭代不定参数
		switch arg.(type) {
		case []CityId_int:
			if len(arg.([]CityId_int)) > 0 {
				flagCity = false
				qs = qs.Filter(m.FN_CityId() + "__in", arg.([]CityId_int))
			}
		}
	}
	if status != -1 {
		qs = qs.Filter(m.FN_Status(), status)
	}
	if city != 0 && flagCity {
		qs = qs.Filter(m.FN_CityId(), city)
	}
	if uid != 0 {
		qs = qs.Filter(m.FN_UserId(), uid)
	}
	return qs.Count()
}